// 构造器参数装饰器
import "reflect-metadata"
type ConstructorParamDecorator = (targetClass: { new (...args: any): any }, paramName: string, paramIndex: number) => void
function InjectConstructor(injectId: any): ParameterDecorator {
  return function(targetClass, paramName, paramIndex) {  // targetClass：类本身
    console.log("targetClass", targetClass)  // [Function: Login]
    console.log("paramName", paramName)  // undefined
    console.log("paramIndex", paramIndex)  // 0
    // 获取当前所有参数类型组成的数组，如果是Class，则直接实例化
    let constructorParamArr = Reflect.getMetadata("design:paramtypes", targetClass)
    console.log("constructorParamArr", constructorParamArr)  // [ [Function: User], [Function: Number], [Function: String] ]
    let constructorParamTypeObj = new constructorParamArr[paramIndex]("管理员");
    instanceObjMap.set(injectId, constructorParamTypeObj)
  }
}

let instanceObjMap = new Map()
class UserService {
  constructor(public name: string) { console.log("创建User实例") }
  showInfo() { console.log("你好，我是", this.name) }
}
class Login {
  user: UserService
  constructor(@InjectConstructor("UserService") user: UserService, age: number, job: string) { this.user = user }
  
  showAdminInfo() {
    let adminObj = instanceObjMap.get("UserService")
    adminObj.showInfo()
  }
}

export {}